<?php
/*
 * Created on Oct 9, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<script type="text/javascript">
$(function() {
	$("a.ajax-edit").click(function(){
		$('#dialog').load($(this).attr('href'));
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 325,
			width: 400,
			title: 'Edit Group',
			modal: true,
			buttons: {
				'Save': function() {
					// Validation
					$('#newsletterGroupForm').ajaxForm({
						beforeSubmit: function(){
							// show waiting
						},
						success: function(data){
							if(data.status=="success"){
								$("#"+data.id + " .item_name").html(data.item_name);
								$("#"+data.id + " .item_description").html(data.item_description);
								$("#"+data.id + " .item_sort").html(data.item_sort);
								alert("You have just updated a record.");
							}
							
						},
						dataType: 'json'
					});
		            $('#newsletterGroupForm').submit();
					$("#dialog").dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				$('#newsletterGroupForm input').val('').removeClass('ui-state-error');
			}
		});
		$("#dialog").dialog("open");
		return false;
	});

	$("a.ajax-delete").click(function(){
		var delLink = $(this).attr("href");
		var temp = new Array(); 
		temp = delLink.split('/');
		id = temp[temp.length-1];
		$("#dialog").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This item will be permanently deleted and cannot be recovered. Are you sure?</p>')
		$("#dialog").dialog({
			title: 'Delete "' + $(this).attr('title') + '"',
			autoOpen: true,
			height: 180,
			buttons: {
				'Delete': function() {
					// Submit delete item
					$.get(delLink, function(){
						alert("You have just removed a record.");
					}, "json");
					$("#"+id).remove();
					$(this).dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}
		});
		$("#dialog").dialog("open");
		return false;
	});

	$('#addGroup').click(function() {
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 300,
			width: 400,
			title: 'Create Group',
			modal: true,
			buttons: {
				'Create': function() {
					// Validation
					$('#newsletterGroupForm').ajaxForm({
						success: function(data){
							if(data.status=="success"){
								alert("You have just insert new record.");
							}
							var $table = $("#groupBrowse tbody");
							$.ajax({
							  url: "<?=site_url(array('c'=>'cms_news','m'=>'get_newest_newsgroup'))?>",
							  cache: false,
							  success: function(html){
							    $table.append(html);
							  }
							});
							$("#dialog").dialog('close');
						},
						dataType: 'json'
					});
		            $('#newsletterGroupForm').submit();
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				$('#newsletterGroupForm input').val('').removeClass('ui-state-error');
			}
		});
		$('#dialog').load($(this).attr('href'));
		$("#dialog").dialog("open");
		return false;
	});
});
</script>
<div style="text-align: right;">
	<a id="addGroup" href="<?=site_url(array("c" => "cms_news", "m" => "add_group"))?>">
		<img src="<?=image_url();?>function/add.gif" width="106" height="31" border="0">
	</a>
</div>
<table id="groupBrowse" class="cont-table-list" cellpadding="5" cellspacing="0" style="width: 100%;">
	<thead>
		<tr>
			<th class="small">Edit</th>
			<th class="small">Delete</th>
			<th>Name</th>
			<th>Description</th>
			<th class="small">Quantity</th>
			<th class="small">Sort</th>
		</tr>
	</thead>
	<tbody>
		<?php if(is_array($groups)): $i = 0; $per_page = $this->pagination->cur_page * $this->pagination->per_page; ?>
		<?php foreach($groups as $group): $i++?>
		<tr class="<?= $i%2 ? 'odd' : 'even'?>" id="<?=$group['id']?>">
			<td class="small"><a class="ajax-edit" href="<?=site_url(array("c" => "cms_news", "m" => "edit_group", "id" => $group['id']))?>" alt="Edit"><img src="<?=image_url()?>icons/edit.gif" alt="Edit"/></a></td>
			<td class="small"><a class="ajax-delete" href="<?=site_url(array("c" => "cms_news", "m" => "delete_group", "id" => $group['id']))?>" title="<?=$group['name']?>" alt="Delete"><img src="<?=image_url()?>icons/del.gif" alt="Delete"/></a></td>
			<td class="item_name"><?=$group['name']?></td>
			<td class="item_description"><?=$group['description']?></td>
			<td class="small"><?=$group['quantity']?></td>
			<td class="small item_sort"><?=$group['sort']?></td>
		</tr>
		<?php endforeach;?>
		<?php endif;?>
	</tbody>
</table>
<div id="temp"></div>
<div class="pagination">
<span>Result: <?=$this->pagination->total_rows?> items</span>
<?= $this->pagination->create_links();?>
</div>

